package com.aries.ui.util;

import com.aries.ui.view.title.ColorStateList;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

/**
 * @Author: AriesHoo on 2018/7/19 9:49
 * @E-Mail: AriesHoo@126.com
 * Function: 资源文件获取帮助类
 * Description:
 */
public class ResourceUtil {
    //系统方法无法单独获取到状态栏高度，只能获取到状态栏高度与底部导航栏高度之和，所以这里给一个固定高度
    private static final int STATUSBAR_DEFAULT_HEIGHT = 72;
    private Context mContext;

    public ResourceUtil(Context context) {
        this.mContext = context;
    }

    public String getText(int res) {
        String txt = null;
        try {
            txt = mContext.getString(res);
        } catch (Exception ignored) {

        }
        return txt;
    }

    public String[] getTextArray(int res) {
        String[] result = new String[0];
        try {
            result = mContext.getStringArray(res);
        } catch (Exception ignored) {
        }
        return result;
    }

    public ShapeElement getDrawable(int res) {
        return new ShapeElement(mContext, res);
    }


    public Element getImageDrawable(int res) {
        PixelMapElement drawable = null;
        try {
            drawable = new PixelMapElement(
                    mContext.getResourceManager().getResource(res));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 通过颜色id 获取elemet对象
     *
     * @param res  res
     * @return  Element
     */
    public Element getColorDrawable(int res) {
        int color = getColor(res);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));

        return shapeElement;
    }

    public ShapeElement getShapeDrawable(int res) {
        return new ShapeElement(mContext, res);
    }
//
//    public PixelMapElement getDrawable(int res) {
//        PixelMapElement drawable = null;
//        try {
//            drawable = new PixelMapElement(
//                    mContext.getResourceManager().getResource(res));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return drawable;
//    }

    public PixelMapElement getPixelMapElement(int res) {
        try {
            return new PixelMapElement(mContext.getResourceManager().getResource(res));
        } catch (Exception e) {
        }
        return null;
    }

    public int getColor(int res) {
        int result = 0;
        try {
            result = mContext.getResourceManager().getElement(res).getColor();
        } catch (Exception e) {
        }
        return result;
    }

    public ColorStateList getColorStateList(int res) {
        return ColorStateList.valueOf(getColor(res));
    }

    public float getDimension(int res) {
        float result = 0;
        try {
            result = mContext.getResourceManager().getElement(res).getFloat();
        } catch (Exception e) {
        }
        return result;
    }

    public int getDimensionPixelSize(int res) {
        int result = 0;
        try {
            result = (int) mContext.getResourceManager().getElement(res).getFloat();
        } catch (Exception ignored) {
        }
        return result;
    }

    public String[] getStringArray(int res) {
        String[] result = new String[0];
        try {
            result = mContext.getResourceManager().getElement(res).getStringArray();
        } catch (Exception ignored) {
        }
        return result;
    }

    public int getAttrColor(int attrRes) {
        int result = 0;
        try {
            result = mContext.getResourceManager().getElement(attrRes).getColor();
        } catch (Exception ignored) {
        }
        return result;
    }

    public float getAttrFloat(int attrRes) {
        return getAttrFloat(attrRes, 1.0f);
    }

    public float getAttrFloat(int attrRes, float def) {
        float result = def;
        try {
            result = mContext.getResourceManager().getElement(attrRes).getFloat();
        } catch (Exception ignored) {
        }
        return result == 0 ? def : result;
    }

    public static PixelMap getPixelMapFromRes(Context context, int res) {
        InputStream stream = null;
        try {
            stream = context.getResourceManager().getResource(res);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(stream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;

            return imageSource.createPixelmap(decodingOptions);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }


    /**
     * 获取状态栏高度 * * @return
     *
     * @param context  context
     * @return 态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        Point point1 = new Point();
        Point point2 = new Point();
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        display.getSize(point1);
        display.getRealSize(point2);
        result = (int) ((double)point2.getPointY() - (double)point1.getPointY());
        return Math.min(result, STATUSBAR_DEFAULT_HEIGHT);
    }
}
